if SERVER then
	AddCSLuaFile("shared.lua")
end

if CLIENT then
	SWEP.PrintName = "Keys"
	SWEP.Slot = 1
	SWEP.SlotPos = 3
	SWEP.DrawAmmo = false
	SWEP.DrawCrosshair = false
end

SWEP.Author = "Sebihunter"
SWEP.Instructions = "(Un)locks doors"
SWEP.Contact = ""
SWEP.Purpose = ""

SWEP.ViewModelFOV = 62
SWEP.ViewModelFlip = false
SWEP.AnimPrefix	 = "rpg"

SWEP.Spawnable = false
SWEP.AdminSpawnable = true
SWEP.Sound = "doors/door_latch3.wav"
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = 0
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = ""

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = 0
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = ""

if CLIENT then
	SWEP.FrameVisible = false
end

function SWEP:Initialize()
	if SERVER then self:SetWeaponHoldType("normal") end
end

function SWEP:Deploy()
	if SERVER then
		self.Owner:DrawViewModel(false)
		self.Owner:DrawWorldModel(false)
	end
end

function SWEP:SecondaryAttack()
	return false
end	

function SWEP:PrimaryAttack()
	if CLIENT then return end

	local trace = self.Owner:GetEyeTrace()

	if ValidEntity(trace.Entity) and (trace.Entity:IsVehicle() or trace.Entity:IsDoor()) and trace.Entity:GetPos():Distance(self.Owner:GetPos()) < 400 then
		if trace.Entity:GetNWString ("owner") == self.Owner:GetNWString("name") or (self.Owner:Team() == (2 or 3 or 4) and trace.Entity:GetNWString ("owner") == "Goverment" ) then
		
			self.Owner:EmitSound(self.Sound)
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.3)
			if trace.Entity:GetNWString ("locked") == 1 then
				trace.Entity:Fire("unlock", "", 0)
				trace.Entity:SetNWInt("locked", 0)
			else
				trace.Entity:Fire("lock", "", 0)
				trace.Entity:SetNWInt("locked", 1)
			end
		else
			if not trace.Entity:IsVehicle() then
				self.Owner:EmitSound("physics/wood/wood_crate_impact_hard2.wav", 100, math.random(90, 110))
			end
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.2)
		end
	end
end